package com.test.servicedemo;
/**引入包*/
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

/**通过bindService和unBindSerivce的方式启动和结束服务*/
public class UseBrider extends AppCompatActivity {
    /**参数设置*/
    CountService countService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new UseBriderFace(this));
        Intent intent = new Intent(UseBrider.this,CountService.class);
        /**进入Activity开始服务*/
        bindService(intent, conn, Context.BIND_AUTO_CREATE);

    }
    private ServiceConnection conn = new ServiceConnection(){
        /**获取服务对象时的操作*/
        /**
             * 只有在Service中的onBind方法中返回一个IBinder实例才会在Bind的时候
             * 调用onServiceConnected回调方法
             * 第二个参数service就是Service中onBind方法return的那个IBinder实例，可以利用这个来传递数据
             */
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            countService = ((CountService.ServiceBinder)service).getService();
            String recStr = ((CountService.ServiceBinder) service).stringToSend;
            Log.i("TAG","The String is : " + recStr);

        }
        /**无法获取到服务对象时的操作*/
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            countService =null;
        }


    };
    protected void onDestroy(){
        super.onDestroy();
        this.unbindService(conn);
        Log.v("MainStadyServics", "out");
    }
}
